package com.lucun.es_transaction.service.impl;


import com.lucun.es_transaction.entity.Admin;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.AdminMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.AdminService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import com.lucun.es_transaction.util.RegexValidateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserLogMapper logMapper;
    /*
    * Admin添加 数据
    * */
    @Override
    public JsonResult<?> insert(Admin record, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(record)) {
            throw new BaseException("没有数据");
        }
        int id = JwtUtil.getIdByToken(request);
        System.out.println(id);
        if(ObjectUtils.isEmpty(id)){
            throw new BaseException("解析token失败");
        }
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isEmpty(tokenNumber)){
            throw new BaseException("解析tokenNumber失败");
        }
        /*
         * 生成 admin_number 管理员编号
         * */
        int num = 0;
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 5; i++) {
            num = random.nextInt(9);
            stringBuffer.append(num);
        }
        record.setAdminNumber(1+stringBuffer.toString());
        record.setCreateTime(new Date());
        /*
        * 抛异常
        * */
        String adminName = record.getAdminName();
        if (StringUtils.isEmpty(adminName)||adminName.length()<1||adminName.length()>12){
            throw new BaseException("管理员姓名不能为空，数位在0~12之间");
        }
        String adminPhone = record.getAdminPhone();
        boolean phone = new RegexValidateUtil().checkMobilePhone(adminPhone);
        if (!phone){
            throw new BaseException("填写正确的手机号");
        }
        String email = record.getAdminEmail();
        boolean b = new RegexValidateUtil().checkEmail(email);
        if (!b){
            throw new BaseException("请填写正确的email格式");
        }
        int i = adminMapper.insertSelective(record);
        //形成添加管理员信息日志
        UserLog log = new UserLog();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(id);
        if(i>0){
            log.setLogName(tokenNumber+"在"+date+"添加成功"+record.getAdminName()+"管理员");
            log.setLogStatus((byte) 1);
            logMapper.insertSelective(log);
        }else{
            log.setLogName(tokenNumber+"在"+date+"添加失败"+record.getAdminName()+"管理员");
            log.setLogStatus((byte) 0);
            logMapper.insertSelective(log);
        }
        if (i>0) {
            return new JsonResult<>("200", "添加成功", "record");
        }else {
            return new JsonResult<>("200", "添加失败", "record");
        }
    }
/*
* 查询Admin 所有
* */
    @Override
    public List<Admin> findAll() {
        return adminMapper.findAll();
    }
    /*
     * 通过编号查询
     * */
    @Override
    public JsonResult<?> findByNumber(HttpServletRequest request) {
        String tokenNumber = JwtUtil.getNumberByToken(request);
        return new JsonResult<>("200","根据编号查询成功",adminMapper.findByNumber(tokenNumber)
        );
    }
/*
* 修改
* */
    @Override
    public JsonResult<?> updateByPrimaryKeySelective(Admin record,HttpServletRequest request) {
        if (ObjectUtils.isEmpty(record)){
            throw new BaseException("没有获取数据");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isEmpty(tokenNumber)){
            throw new BaseException("解析tokenNumber失败");
        }
        String adminPhone = record.getAdminPhone();
        if (!StringUtils.isEmpty(adminPhone)) {
            boolean phone = new RegexValidateUtil().checkMobilePhone(adminPhone);
            if (!phone) {
                throw new BaseException("填写正确的手机号");
            }
        }
        String email = record.getAdminEmail();
        if (!StringUtils.isBlank(email)) {
            boolean b = new RegexValidateUtil().checkEmail(email);
            if (!b) {
                throw new BaseException("请填写正确的email格式");
            }
        }
        record.setUpdateTime(new Date());
        int i = adminMapper.update(record);
        //形成修改管理员信息日志
        UserLog log = new UserLog();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 1);
        log.setUserId(idByToken);
        if(i>0){
            log.setLogName(tokenNumber+"在"+date+"修改成功"+record.getAdminName()+"管理员");
            log.setLogStatus((byte) 1);
            logMapper.insertSelective(log);
        }else{
            log.setLogName(tokenNumber+"在"+date+"修改失败"+record.getAdminName()+"管理员");
            log.setLogStatus((byte) 0);
            logMapper.insertSelective(log);
        }
        if (i>0) {
            return new JsonResult<>("200", "修改成功", "record");
        }else {
            return new JsonResult<>("500", "修改失败", "record");
        }
    }
/*
* 根据id删除
* */
    @Override
    public JsonResult<?> deleteByPrimaryKey(int id,HttpServletRequest request) {
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("没有获取此id");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        String tokenNumber = JwtUtil.getNumberByToken(request);
        if (StringUtils.isEmpty(tokenNumber)){
            throw new BaseException("解析tokenNumber失败");
        }
        int i = adminMapper.deleteByPrimaryKey(id);
        //形成删除管理员信息日志
        UserLog log = new UserLog();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 2);
        log.setUserId(idByToken);
        if(i>0){
            log.setLogName(tokenNumber+"在"+date+"删除成功");
            log.setLogStatus((byte) 1);
            logMapper.insertSelective(log);
        }else{
            log.setLogName(tokenNumber+"在"+date+"删除失败");
            log.setLogStatus((byte) 0);
            logMapper.insertSelective(log);
        }
        if (i>0) {
            return new JsonResult<>("200", "删除成功", "record");
        }else {
            return new JsonResult<>("500", "删除失败", "record");
        }
    }
}
